﻿Imports System.Deployment.Application
Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            logger.Info("App end")
        End Sub
        Private logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger
        Public STAScheduler As New System.Threading.Tasks.Schedulers.StaTaskScheduler(1)


        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim sta As Boolean = Threading.Thread.CurrentThread.TrySetApartmentState(Threading.ApartmentState.STA)
            logger.Info("App start")
            DevExpress.UserSkins.BonusSkins.Register()
            DevExpress.UserSkins.BonusSkins.Register()
            DevExpress.Skins.SkinManager.EnableFormSkins()


            Try
                IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "VBJay Solutions\Employee Scheduler"))
            Catch ex As Exception

            End Try

            If ApplicationDeployment.IsNetworkDeployed Then
                If ApplicationDeployment.CurrentDeployment.UpdatedVersion > ApplicationDeployment.CurrentDeployment.CurrentVersion Then
                    If IO.Directory.Exists(ApplicationDeployment.CurrentDeployment.DataDirectory & "\.pre") Then
                        CopyKeys(ApplicationDeployment.CurrentDeployment.DataDirectory & "\.pre", ApplicationDeployment.CurrentDeployment.DataDirectory)
                        CopyData(ApplicationDeployment.CurrentDeployment.DataDirectory & "\.pre", ApplicationDeployment.CurrentDeployment.DataDirectory)

                    End If

                End If

            End If

            If My.Settings.ShouldUpgrade Then
                My.Settings.Upgrade()
                My.Settings.ShouldUpgrade = False
            End If


        End Sub

        Private Sub CopyKeys(ByVal oldPth As String, ByVal NewPth As String)
            If IO.File.Exists(IO.Path.Combine(oldPth, My.Resources.BossFile)) Then
                Try
                    IO.File.Copy(IO.Path.Combine(oldPth, My.Resources.BossFile), IO.Path.Combine(NewPth, My.Resources.BossFile), False)
                Catch ex As Exception

                End Try


            End If
        End Sub
        Private Sub CopyData(ByVal oldPth As String, ByVal NewPth As String)
            If IO.File.Exists(IO.Path.Combine(oldPth, My.Resources.EmployeeData)) Then
                Try
                    IO.File.Copy(IO.Path.Combine(oldPth, My.Resources.EmployeeData), IO.Path.Combine(NewPth, My.Resources.EmployeeData), False)
                Catch ex As Exception

                End Try


            End If
        End Sub

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            Dim frm As frmEmployees = (From f In Me.OpenForms Where TypeOf f Is frmEmployees).FirstOrDefault
            frm.Focus()


        End Sub

        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            If System.Diagnostics.Debugger.IsAttached Then Return
            logger.ErrorException("Unhandled exception.", e.Exception)
            MessageBox.Show(e.Exception.ToString, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End

        End Sub
    End Class

End Namespace

